package com.cloudptt.wp.ui.views;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.json.JSONObject;


public class EditLayout extends LinearLayout implements OnClickListener{

	public static final String TAG = "EditLayout";
	private static final int MAX_CONTENT_LEN = 128;
	public static final int SHOW_EDIT = 0;
	public static final int SHOW_PTT =1;

	public static final int CHOOSE_PIC = 0;
	public static final int OPEN_CAMERA = 1;

	private Context context = null;
	//	private LinearLayout editLayout;

	private ImageView switchToPttIV;
	private ImageView switchToRecordIV;
	private ImageView switchToEditIV;

	private ImageView sendTextBtn;

	private EditText contentEditText;

	//	private FrameLayout recordLayout;
	//	private ImageView recordBgView;
	//	private TextView recordTextView;

	private ImageView mChoosePic;
	private ImageView mTakePhoto;

	private ImageView mPcall;
	private ImageView mConference;

	private ImageView videoReturn;
	private ImageView videoPull;

	private ImageView mChooseFile;

//	private AudioRecorderButton audioRecorderButton;

	private LinearLayout mExpandFuncLayout;
	private InputMethodManager imm;
	private ImageView mAddFuncIv;

	private boolean isShowExpand = false;
	//	private GCallEventAdapter mCallEventAdapter = null;

	private AddrInfo addrInfo = null;
	private EditLyChangeListener listener = null;

	public EditLayout(Context context) {
		this(context, null);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		this(context, attrs, defStyleAttr, 0);
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public EditLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		// TODO Auto-generated constructor stub
		this.context = context;

		LayoutInflater.from(context).inflate(R.layout.bottom_edit_layout, this);

		//		editLayout = (LinearLayout)findViewById(R.id.bottom_edit_ll);
		mExpandFuncLayout = (LinearLayout)findViewById(R.id.expand_func_layout);

		switchToPttIV = (ImageView)findViewById(R.id.switch_ptt);
		switchToPttIV.setOnClickListener(this);

		switchToRecordIV = (ImageView)findViewById(R.id.switch_record);
		switchToRecordIV.setOnClickListener(this);

		switchToEditIV = (ImageView)findViewById(R.id.switch_edit);
		switchToEditIV.setOnClickListener(this);

		sendTextBtn=(ImageView) findViewById(R.id.send_text_msg);
		sendTextBtn.setOnClickListener(this);

		mAddFuncIv = (ImageView)findViewById(R.id.add_func);
		mAddFuncIv.setOnClickListener(this);

		mChoosePic = (ImageView)findViewById(R.id.choose_pic_iv);
		mChoosePic.setOnClickListener(this);

		mTakePhoto = (ImageView) findViewById(R.id.camera_iv);
		mTakePhoto.setOnClickListener(this);

		mPcall = (ImageView) findViewById(R.id.call_iv);
		mPcall.setOnClickListener(this);


		videoReturn = (ImageView) findViewById(R.id.videoReturn);
		videoReturn.setOnClickListener(this);

		videoPull = (ImageView) findViewById(R.id.videoPull);
		videoPull.setOnClickListener(this);

		mConference = (ImageView) findViewById(R.id.conference_iv);
		mConference.setOnClickListener(this);

		//mChooseFile = findViewById(R.id.)

		contentEditText=(EditText) findViewById(R.id.contentEditText);
		//contentEditText.requestFocus();

//		audioRecorderButton=(AudioRecorderButton)findViewById(R.id.record_button);
//		audioRecorderButton.setAudioFinishRecorderListener(new AudioRecorderButton.AudioFinishRecorderListener() {
//			@Override
//			public void onFinish(float time, String filePath) {
//				if(listener != null)
//				{
//
//					File file = new File(filePath);
//					String fileName = file.getName();
//					time = time + 1;
//					int val = (int)time;
//					Log.d(TAG, "audioRecorderButton onFinish time = " + time + " val = " + val + " fileName = " + fileName);
//					listener.showUploadDialog(file,val);
//				}
//			}
//		});


		imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);  
		contentEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean isFocus) {
				// TODO Auto-generated method stub
				Log.d(TAG, "isEdittext focus = "+isFocus);
				if(isFocus){
					imm.showSoftInputFromInputMethod(contentEditText.getWindowToken(), 0);
					if(mExpandFuncLayout.isShown()){
						mExpandFuncLayout.setVisibility(View.GONE);
					}

				}else{
					imm.hideSoftInputFromWindow(contentEditText.getWindowToken(), 0); 
				}
			}
		});
		contentEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub
				String str = contentEditText.getText().toString();
				if(str != null
						&& str.length() > 0)
				{
					switchToRecordIV.setVisibility(View.GONE);
					sendTextBtn.setVisibility(View.VISIBLE);
				}
				else
				{
					switchToRecordIV.setVisibility(View.VISIBLE);
					sendTextBtn.setVisibility(View.GONE);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	public void setListener(AddrInfo addrInfo,EditLyChangeListener listener) {
		this.addrInfo = addrInfo;
		this.listener = listener;
	}

	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		SysApplication app = ((SysApplication) context.getApplicationContext());
		Product product = app.getProduct();

		switch (view.getId()) {
		case R.id.switch_ptt:
			if(listener != null)
			{
				listener.onSwitchPttLy();
			}
			break;

		case R.id.switch_record:
			contentEditText.setVisibility(View.GONE);
			switchToRecordIV.setVisibility(View.GONE);

			switchToEditIV.setVisibility(View.VISIBLE);
//			audioRecorderButton.setVisibility(View.VISIBLE);
			break;

		case R.id.switch_edit:
			contentEditText.setVisibility(View.VISIBLE);
			contentEditText.requestFocus();
			imm.showSoftInput(contentEditText, 0);
			switchToRecordIV.setVisibility(View.VISIBLE);

			switchToEditIV.setVisibility(View.GONE);
//			audioRecorderButton.setVisibility(View.GONE);
			break;

		case R.id.send_text_msg:
			String contentStr = contentEditText.getText().toString();
			JSONObject json = new JSONObject();
			try
			{
				json.put("SDS", contentStr);
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			int len = CheckData.getStrLen(contentStr);
			if (contentStr.length() <= 0
					|| MAX_CONTENT_LEN < len)
			{
				CommDlg.showToast(context, getResources().getString(R.string.msg_content_err));
				return;
			}
			if(listener != null)
			{
				listener.onSendMsg(SessionInfo.ContentType_Text,json.toString(),0);
			}
			contentEditText.setText("");
			contentEditText.requestFocus();
			break;

		case R.id.add_func:
			isShowExpand = !isShowExpand;
			showExpandFunc(isShowExpand);
			break;

		case R.id.choose_pic_iv:
			listener.doAction(CHOOSE_PIC);
			break;

		case R.id.camera_iv:
			listener.doAction(OPEN_CAMERA);
			break;

		case R.id.videoReturn:
//			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Video_Push,true);
			break;
		case R.id.videoPull:
//			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Video_Pull,true);
			break;
		case R.id.call_iv:
		case R.id.conference_iv:
//			product.getPCallEventAdapter().makePrivateCall(addrInfo, CallAttr.CallType_Conference,false);
			break;

		default:
			break;
		}
	}

	public void clearState(){
		showExpandFunc(false);
		contentEditText.clearFocus();
	}

	private void showExpandFunc(boolean isShow)
	{
		if(isShow)
		{
			contentEditText.clearFocus();
			mExpandFuncLayout.setVisibility(View.VISIBLE);

		}
		else
		{
			mExpandFuncLayout.setVisibility(View.GONE);
			contentEditText.requestFocus();
		}
	}

	public void setSwitchPTTEnable(boolean isPTTenable){
		switchToPttIV.setVisibility(View.GONE);
	}
}
